<template>
  <div>
    <div class="tool-bar">
      <el-row :gutter="20" class="h_top">
        <el-form ref="form" inline label-width="82px" label-position="left" class="nb-form">
          <el-col :span="4">
            <el-form-item label="项目名称：">
              <el-input v-model="query.name" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="所属客户：">
              <v-select-s v-model="query.customer_id" action="customerList" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="状态：">
              <el-select v-model="query.status" @keyup.enter.native="search">
                <el-option v-for="(item, statusIndex) in projectStatus" :key="statusIndex" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="userId === 1" :span="4">
            <el-form-item label="负责人：">
              <v-select v-model="query.create_admin_id" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="开始时间：">
              <el-date-picker v-model="query.start_time" type="daterange" placeholder="" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row :gutter="6" style="margin-top: 30px" class="bar-btn">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">新增</el-button>
        <el-button type="primary" icon="el-icon-upload2" class="button-hidden">导出</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="delBtn">删除</el-button>
        <el-button type="primary" icon="el-icon-refresh-right" @click="refresh">刷新</el-button>
      </el-row>
    </div>
    <div class="nb-content">
      <el-table ref="multipleTable" v-loading="loading" border :data="data.dataList" tooltip-effect="dark" class="nb-table" width="100%" @selection-change="handleSelectionChange">
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="name" label="项目名称" align="center" show-overflow-tooltip width="280" />
        <el-table-column prop="customer.name" label="所属客户" align="center" show-overflow-tooltip width="280" />
        <el-table-column prop="status" label="状态" :formatter="formatter" align="center" width="130" />
        <el-table-column prop="cr_admin.name" label="负责人" align="center" />
        <el-table-column prop="start_time" label="开始时间" align="center" width="180" />
        <el-table-column prop="content" label="备注" align="center" />
        <el-table-column label="操作" fixed="right" align="center" width="180">
          <template scope="{row}">
            <el-button type="primary" @click="goDetail(row.id)">编辑</el-button>
            <el-button type="danger" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="data.total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>

<script>
import API from '@/api/project/project'
import { projectStatus } from '@/utils/options'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
import { mapGetters } from 'vuex'
export default {
  inject: ['reload', 'goUrl', 'projectFormatter'],
  components: { VSelect, VSelectS },
  data() {
    return {
      data: {
        dataList: [
        ],
        total: 0
      },
      query: {
        limit: 15,
        page: 1
      },
      loading: true,
      multipleSelection: [],
      projectStatus
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      this.loading = true
      API.projectList(this.query).then(res => {
        this.data.dataList = res.data.data
        this.data.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    formatter(row) {
      return this.projectFormatter(row)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    goDetail(id) {
      const url = `/project/edit/${id}`
      this.goUrl(url)
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete({ id }).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('删除成功')
            this.reload()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delBtn() {
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    },
    handleSearch() {
      this.query.page = 1
      this.index()
    },
    refresh() {
      this.reload()
    },
    add() {
      this.goUrl('/project/add')
    },
    getName(arr, value) {
      const res = arr.find(item => {
        if (item.value === value) {
          return item
        }
      })
      3
      if (res && res.label) {
        return res.label
      } else {
        return '未知'
      }
    }
  }
}
</script>

<style>
.nb-form .el-form-item__content{
  width: calc(100% - 82px);
}
</style>
